package com.enniswu.weitter.user.start;


import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BaseFragment;
import com.enniswu.weitter.user.update.UpdateInfoActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterFragment extends BaseFragment<StartContract.Presenter> implements StartContract.View {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.bt_code)
    Button btCode;
    private StartFragmentListener listener;

    @Override
    protected int setContentView() {
        return R.layout.fragment_register;
    }

    @Override
    protected void onCreateView(View contentView) {
        presenter = new StartPresenter(this);
    }

    @Override
    public void next(int id) {
        if (getActivity() != null) {
            Intent intent = new Intent(getContext(), UpdateInfoActivity.class);
            intent.putExtra("userId", id);
            startActivity(intent);
            getActivity().finish();
        }
    }

    @OnClick(R.id.bt_register)
    public void register() {
        presenter.register(etPhone.getText().toString(),
                etPsw.getText().toString(), etCode.getText().toString());
    }

    @OnClick(R.id.bt_code)
    public void sendCode() {
        presenter.sendCode(etPhone.getText().toString());
        new MyCountDownTimer(60 * 1000, 1000).start();
    }

    @OnClick(R.id.tv_login)
    public void toLogin() {
        listener.changeItem(0);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof StartFragmentListener) {
            listener = (StartFragmentListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " 必须实现 StartFragmentListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            btCode.setClickable(false);
            btCode.setTextColor(getResources().getColor(R.color.unable));
            btCode.setText(String.format(getResources().getString(R.string.resend_code),
                    millisUntilFinished / 1000));
            btCode.setBackground(getResources().getDrawable(R.drawable.bg_bt_start_stroke_unable));
        }

        @Override
        public void onFinish() {
            btCode.setTextColor(getResources().getColor(R.color.colorAccent));
            btCode.setText(R.string.send_code);
            btCode.setClickable(true);
            btCode.setBackground(getResources().getDrawable(R.drawable.bg_bt_start_stroke));
        }
    }
}
